<?php

namespace App\Http\Controllers\Admin\Configs;

use Illuminate\Http\Request;
use App\Http\Controllers\AdminController;

use App\Models\Admin\Role;
use DB;

class RoleController extends AdminController
{

    /**
     * 获取列表数据
     * @return [type] [description]
     */
    function getLists(Request $request){
        $pageIndex = (int)$request->input('page') ?  $request->input('page') : 1;
        $length    = (int)$request->input('length') ?  $request->input('length') : 10;
        $start     = ($pageIndex-1)*$length;

    	$where = [];
    	$data['total'] = Role::where($where)->count();
    	if($data['total']>0){
    		$data['result'] = Role::where($where)->orderBy('name','desc')->skip($start)->take($length)->get()->toArray();
    	}
    	$this->ajaxJson(0,'',$data);
    }

    /**
     * 获取单个角色信息
     * @return [type] [description]
     */
    function getRole($id){
    	$data = Role::where('id',$id)->with('permissions')->first();
    	$this->ajaxJson(0,'',$data);
    }

    /**
     * 获取所有角色
     * @return [type] [description]
     */
    function getAll(){
        $this->ajaxJson(0,'',Role::orderBy('name','desc')->orderBy('updated_at','desc')->get()->toArray());
    }

    /**
     * 验证角色名唯一
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    function checkName(Request $request){
    	$id = $request->input('id');
    	$name = $request->input('name');
    	$this->checkNameUnique($id,$name);
    	$this->ajaxJson();
    }

    /**
     * 更新和添加数据
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    function postUpdate(Request $request){
    	$id = $request->input('id');
    	$name = $request->input('name');
    	$this->checkNameUnique($id,$name);
    	if($id){
    		$role = $this->_get_role($id);
    	}else{
    		$role = new Role();
    	}
        $role->name        = $name;
        $role->displayName = $request->input('displayName');
        $role->description = $request->input('description');
    	$role->save();
        $role->permissions()->sync($request->input('permissions'));
    	$this->ajaxJson(0,'更新成功');
    }

    /**
     * 删除角色
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    function deleteOne($id){
    	$role = $this->_get_role($id);
    	$role->delete();
    	$role->permissions()->detach();
    	$this->ajaxJson(0,'删除成功');
    }

    /**
     * 验证角色英文名唯一性
     * @param  [int] $id   [角色主键ID]
     * @param  [string] $name [主键英文名]
     * @return [type]       [description]
     */
    private function checkNameUnique($id,$name){
    	$unique = Role::where('id','!=',$id)->where('name',$name)->count();
		if($unique>0){
			$this->ajaxJson(1,'角色名已存在');
		}
		return false;
    }

    /**
     * 获取角色
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    private function _get_role($id){
    	$role = Role::find($id);
    	if(!$role){
    		$this->ajaxJson(1,'请求角色数据不存在');
    	}
    	return $role;
    }

    /**
     * 获取单个权限
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    private function _get_permission($id){
    	$permission = Permission::find($id);
    	return $permission;
    }
}
